package com.library.dao.impl;

import com.library.bean.Book;
import com.library.dao.BaseDao;
import com.library.dao.BookDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BookDaoimpl implements BookDao {

    @Override
    public List<Book> SelectAll() {
        Book book=new Book();
        List<Book> bookList = new ArrayList<Book>();
        Connection con=null;
        PreparedStatement pstm =null;
        ResultSet res =null;
        String sql = "SELECT * FROM book";
        try {
            con = BaseDao.getConnection();
            pstm = con.prepareStatement(sql);
            res = pstm.executeQuery();
            while (res.next()) {
                book=new Book();
                book.setBookId(res.getInt("bookId"));
                book.setBookName(res.getString("bookName"));
                book.setBookType(res.getString("bookType"));
                book.setBookWriter(res.getString("bookWriter"));
                bookList.add(book);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.closeAll(con, pstm, res);
        }
        return bookList;
    }
}
